<?php
/**
 * @file WCReadRate.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-07 02:16:25 +0000 (Fri, 07 Jun 2013) $
 *
 * $Id: WCReadRate.php 71 2013-06-07 02:16:25Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/includes/WCReadRate.php $
 */

/**
 * @class WCReadRate
 */
class WCReadRate {
	/**
	 * @var WCReadRate
	 */
	protected static	$_Instance;

	/**
	 * @var WCConnection
	 */
	protected	$_db;
	/**
	 * @var string
	 */
	protected	$_dbprefix;
	/**
	 * @var int
	 */
	protected	$_group;
	/**
	 * @var int
	 */
	protected	$_user;

	protected function __construct() {
		global	$wcDatabaseAccess;
		global	$wcGroup;
		global	$wcUser;

		$this->_db		= WCConnection::Instance();
		$this->_dbprefix	= $wcDatabaseAccess['prefix'];
		$this->_group		= $wcGroup;
		$this->_user		= $wcUser;
	}
	public function __destruct() {
	}

	//
	// Public methods
	public function comicRate(WCComic &$comic) {
		$out = $this->comicReadRate($comic);
		return $out["rate"];
	}
	public function comicReadRate(WCComic &$comic) {
		$out = array(
				"rate"	=> $comic->rate,
				"reads"	=> $comic->reads,
				"votes"	=> $comic->votes
		);

		return $out;
	}
	public function comicReads(WCComic &$comic) {
		$out = $this->comicReadRate($comic);
		return $out["reads"];
	}
	public function rate(WCComic &$comic, WCUser &$user=null) {
		$out = $this->readRate($comic, $user);
		return $out["rate"];
	}
	public function readRate(WCComic &$comic, WCUser &$user=null) {
		$out = array(
				'rate' => array(
						'rate'	=> false,
						'date'	=> false),
				'read' => array(
						'read'	=> false,
						'date'	=> false));

		$group	= $comic->group();
		$user	= ($user === null) ? $this->_user  : $user;

		if($user !== false) {
			$query = "select  crr_rate,\n";
			$query.= "        crr_rate_date,\n";
			$query.= "        crr_read,\n";
			$query.= "        crr_read_date\n";
			$query.= "from    {$this->_dbprefix}read_rates\n";
			$query.= "where   crr_user  = :user\n";
			$query.= " and    crr_group = :group\n";
			$query.= " and    crr_comic = :comic\n";
			$stmt = $this->_db->prepare(__CLASS__."::".__FUNCTION__, $query);

			$params = array(
					":user"		=> $user->id(),
					":group"	=> $group->id,
					":comic"	=> $comic->id);
			if($stmt->execute($params) && $stmt->rowCount() > 0) {
				$row = $stmt->fetch();

				$out["rate"]["rate"] = $row["crr_rate"];
				$out["rate"]["date"] = $row["crr_rate_date"];
				$out["read"]["read"] = $row["crr_read"] == "Y";
				$out["read"]["date"] = $row["crr_read_date"];
			}
		}

		return $out;
	}
	public function read(WCComic &$comic, WCUser &$user=null) {
		$out = $this->readRate($comic, $user);
		return $out["read"];
	}
	public function setRate(WCComic &$comic, $rate, WCUser &$user=null) {
		$out = false;

		$group	= $comic->group();
		$user	= ($user === null) ? $this->_user  : $user;

		if($user !== false) {
			$query = "insert\n";
			$query.= "        into {$this->_dbprefix}read_rates (\n";
			$query.= "                crr_user, crr_group, crr_comic, crr_rate, crr_rate_date)\n";
			$query.= "        values (:user, :group, :comic, :rate, sysdate())\n";
			$query.= "        on duplicate key\n";
			$query.= "                update  crr_rate      = :rate,\n";
			$query.= "                        crr_rate_date = sysdate()\n";
			$stmt = $this->_db->prepare(__CLASS__."::".__FUNCTION__, $query);

			$params = array(
					":user"		=> $user->id(),
					":group"	=> $group->id,
					":comic"	=> $comic->id,
					":rate"		=> $rate);
			$stmt->execute($params);

			$out = true;
		}

		return $out;
	}
	public function setRead(WCComic &$comic, $read=true, WCUser &$user=null) {
		$out = false;

		$group		= $comic->group();
		$user		= ($user === null) ? $this->_user  : $user;
		$crrRead	= $read ? "Y" : "N";

		if($user !== false) {
			$query = "insert\n";
			$query.= "        into {$this->_dbprefix}read_rates (\n";
			$query.= "                crr_user, crr_group, crr_comic, crr_read, crr_read_date)\n";
			$query.= "        values (:user, :group, :comic, :read, sysdate())\n";
			$query.= "        on duplicate key\n";
			$query.= "                update  crr_read      = :read,\n";
			$query.= "                        crr_read_date = sysdate()\n";
			$stmt = $this->_db->prepare(__CLASS__."::".__FUNCTION__, $query);

			$params = array(
					":user"		=> $user->id(),
					":group"	=> $group->id,
					":comic"	=> $comic->id,
					":read"		=> $crrRead);
			$stmt->execute($params);

			$out = true;
		}

		return $out;
	}
	public function updateStats() {
		//
		// Updating comic ratings.
		$stmt	= null;
		$stmtId	= __CLASS__."::".__FUNCTION__;
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "update  {$this->_dbprefix}comics\n";
			$query.= "set     cmc_reads = (\n";
			$query.= "                select  count(1)\n";
			$query.= "                from    {$this->_dbprefix}read_rates\n";
			$query.= "                where   crr_group     = cmc_group\n";
			$query.= "                 and    crr_comic     = cmc_id\n";
			$query.= "                 and    crr_read      = 'Y'),\n";
			$query.= "        cmc_rate  = (\n";
			$query.= "                select  avg(crr_rate)\n";
			$query.= "                from    {$this->_dbprefix}read_rates\n";
			$query.= "                where   crr_group     = cmc_group\n";
			$query.= "                 and    crr_comic     = cmc_id),\n";
			$query.= "        cmc_votes = (\n";
			$query.= "                select  count(*)\n";
			$query.= "                from    {$this->_dbprefix}read_rates\n";
			$query.= "                where   crr_group     = cmc_group\n";
			$query.= "                 and    crr_comic     = cmc_id),\n";
			$query.= "        cmc_rated = 'Y'\n";
			$query.= "where   cmc_rated = 'N'\n";
			$query.= " and    cmc_lost  = 'N'\n";
				
			$stmt = $this->_db->prepare($stmtId, $query);
		}
		$stmt->execute();

		return $stmt->rowCount();
	}

	//
	// Protected methods

	//
	// Public class methods
	public static function Instance() {
		if(!isset(self::$_Instance)) {
			$c = __CLASS__;
			self::$_Instance = new $c();
		}

		return self::$_Instance;
	}
}
?>
